package day10.snake;

import java.util.HashSet;
import java.util.LinkedList;
import java.util.Scanner;
import java.util.Set;

public class SnakeDemo {

    public static final int ROWS = 20;

    public static final int COLS = 20;

    private Snake snake;

    public Snake getSnake(){
        return snake;
    }

    Set<Nodes> foods = new HashSet<>();

    public SnakeDemo(){
    this.snake = new Snake();
    initFoods(3);
    }

    public void initFoods(int size){
        while(true){
            int m = (int)(Math.random()*(ROWS-2)+1);
            int n = (int)(Math.random()*(COLS-2)+1);

            if(snake.contains(m,n)){
                continue;
            }

            foods.add(new Nodes(m,n));

            if(foods.size()==size)
                break;
        }
    }

    public void printPanel(){
        for(int i = 0;i<ROWS;i++){
            for(int j = 0;j<COLS;j++){
                if(i==0 || i==ROWS-1 || j==0 || j==COLS-1){
                    System.out.printf("%2s","*");
                }else if(snake.contains(i,j)){
                    System.out.printf("%2s","#");
                }else if(foods.contains(new Nodes(i,j))){
                    System.out.printf("%2s","0");
                }else{
                    System.out.printf("%2s"," ");
                }
            }
            System.out.println();
        }
    }

    public class Snake{
        LinkedList<Nodes> snakes = new LinkedList<>();
        public static final int UP = -1;
        public static final int DOWN = 1;
        public static final int LEFT = -2;
        public static final int RIGHT = 2;

        public int dir;

        public Snake(){
            snakes.add(new Nodes(5,10));
            snakes.add(new Nodes(5,11));
            snakes.add(new Nodes(5,12));
            snakes.add(new Nodes(5,13));

            dir = -2;
        }

        public boolean contains(int m,int n){
            Nodes nodes = new Nodes(m,n);
            return snakes.contains(nodes);
        }

        public void step(int dir){
            if(this.dir + dir == 0)
                throw new RuntimeException("sorry!不能碰到自己！");
            this.dir = dir;
            step();
        }

        public void step(){
            Nodes head = snakes.getFirst();
            int m = head.getM();
            int n = head.getN();
            switch(dir){
                case UP:
                    m--;
                    break;
                case DOWN:
                    m++;
                    break;
                case LEFT:
                    n--;
                    break;
                case RIGHT:
                    n++;
                    break;
            }
            head = new Nodes(m,n);

            snakes.addFirst(head);

            if(foods.remove(head)){
                return;
            }

            snakes.removeLast();
        }
    }
}
class StarGame{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);

        SnakeDemo s = new SnakeDemo();

        SnakeDemo.Snake snake = s.getSnake();
        do{
            s.printPanel();
            System.out.println("w->上 s->下 a-左 d->右 enter");
            System.out.println(">");
            String dir = sc.nextLine();
            switch(dir) {
                case "w":
                    snake.step(SnakeDemo.Snake.UP);
                    break;
                case "s":
                    snake.step(SnakeDemo.Snake.DOWN);
                    break;
                case "a":
                    snake.step(SnakeDemo.Snake.LEFT);
                    break;
                case "d":
                    snake.step(SnakeDemo.Snake.RIGHT);
                    break;
                default:
                    snake.step();
            }

        }while(true);
    }
}
